//C1相接D2，C2相接D3
#include <TimerOne.h>

#define PULSE_NUMBER 11
#define RATIO 54
#define RADIUS 0.0325
#define PAI 3.1415926
#define SET_TIME 0.005


double encoder=0;
double encoder_last=0;
double encoder_minus=0;
double carspeed=0;
double caromega=0;
double counttime=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  Serial.begin(9600);

  attachInterrupt(0, onChange1, RISING);
//  attachInterrupt(1, onChange2, RISING);

  Serial.println("Init Int over");

  Timer1.initialize(5000);
  Timer1.attachInterrupt(TimeChange);

  Serial.println("Init Time over");
  
}

void onChange1(){
  encoder++;
  //Serial.println("encoder1 ++");
}
/*
void onChange2(){
  encoder++;
  Serial.println("encoder2 ++");
}
*/
void TimeChange(){
  encoder_minus = encoder - encoder_last;
  encoder_last = encoder;
}

void loop() {
  // put your main code here, to run repeatedly:
  caromega = (encoder_minus * 2 * PAI) / (PULSE_NUMBER * RATIO * SET_TIME);
  carspeed = caromega * RADIUS;
  Serial.print("The Speed is: ");
  Serial.print(carspeed);
  Serial.print(" m/s.\n");

  

}
